﻿using Mapster;
using Microsoft.AspNetCore.Mvc;
using BJ.AuthEntity.Models;
using BJ.AuthEntity.ViewModels;
using Syspetro.Core.AppDeal;
using Syspetro.Core.DynamicApi;
using Syspetro.Deploy.AuthorizationReq;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using BJ.AuthDomain.IService;
using Serilog;
using Microsoft.AspNetCore.Authorization;

namespace BJ.AuthApp.Admin
{
    /// <summary>
    /// 角色管理
    /// </summary>
    [Authorize]
    [Admin]
    public class RoleApp : BaseDynamicApi
    {
        readonly IRoleService RoleService;
        readonly IMenuService  MenuService;

        public RoleApp(IRoleService roleService, IMenuService menuService)
        {
            RoleService = roleService;
            MenuService = menuService;

        }
        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="vrole"></param>
        /// <returns></returns>
        public async Task<SpActionResult<Guid>> AddRole(VM_Role_Add vrole)
        {
            if (string.IsNullOrEmpty(vrole.Name))
                return Fail<Guid>("名称不能为空");
            return Result(await RoleService.AddAsync(vrole.Adapt<AdminRole>()));
        }
        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="vrole"></param>
        /// <returns></returns>
        public async Task<SpActionResult<int>> AddRoles(List<VM_Role_Add> vrole)
        {
            return Result(await RoleService.AddAsync(vrole.Adapt<List<AdminRole>>()));
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<SpActionResult<bool>> DeleteRole(Guid id)
        {
            return Result(await RoleService.DeleteAsync(id));
        }
        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="role_Update"></param>
        /// <returns></returns>
        public async Task<SpActionResult<bool>> UpdateRole(VM_Role_Update role_Update)
        {
            return Result(await RoleService.UpdateAsync(role_Update, t => t.Id == role_Update.Id));
        }
        /// <summary>
        /// 查看角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<SpActionResult<VM_Role>> GetRole(Guid id)
        {
            var list = await RoleService.GetAsync(id);
            return Result(list.Adapt<VM_Role>());
        }
        /// <summary>
        /// 查找所有角色
        /// </summary>
        /// <returns></returns>
        [ProducesResponseType(typeof(VM_Role), 200)]
        public async Task<SpActionResult<List<VM_Role>>> GetRoles()
        {
           
            return Result(await RoleService.FindDtoAsync<VM_Role>(d=>true));
        }
        /// <summary>
        /// 查找角色下菜单
        /// </summary>
        /// <returns></returns>
        [ProducesResponseType(typeof(VM_RoleMenu), 200)]
        public async Task<SpActionResult<List<VM_RoleMenu>>> GetRoleMenus(Guid roleId)
        {

            return Result(await MenuService.GetRoleMenus(roleId));
        }

    }
}
